package com.fivestar.pmanager.web.listener;

import java.io.IOException;

import javax.servlet.ServletContext;

import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;
import org.springframework.web.context.support.XmlWebApplicationContext;

import com.fivestar.pmanager.core.util.LoggerHelper;
import com.fivestar.pmanager.service.SysDictService;


/**
 * spring 启动完成加载字典
 *
 * Create by admin
 */
@Component
public class StartupListener implements ApplicationListener<ContextRefreshedEvent>
{
    private static final org.slf4j.Logger logger = LoggerFactory.getLogger(StartupListener.class);

    @Autowired
    private SysDictService sysDictService;

    @Override
    public void onApplicationEvent(ContextRefreshedEvent arg0)
    {
        logger.debug("spring mvc done");
        ServletContext servletContext = ((XmlWebApplicationContext)arg0.getApplicationContext()).getServletContext();
        logger.debug("init dict_js........");
        generateJs(servletContext);

    }

    private void generateJs(ServletContext servletContext)
    {
        try
        {
            String jsDir = servletContext.getRealPath("/assets/global/scripts/SM_DICT.js");
            sysDictService.generateJs(jsDir);
        }
        catch (IOException e)
        {
            LoggerHelper.logError(logger, e);
        }
    }

}
